﻿using Newtonsoft.Json;
using SaleManagement.Models;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace SaleManagement.Common
{
    public class Template
    {
        //teamplate select
        public static T getData<T>(String url) where T : class
        {
            T objreturn;
            using (WebClient client = new WebClient())
            {
                String value = client.DownloadString(url);//lay json tu web ve
                objreturn = JsonConvert.DeserializeObject<T>(value);
            }
            return objreturn;
        }
        //
        //teamplate Insert
        public static void Create<T>(T obj, string url) where T : class
        {
            using (WebClient client = new WebClient())
            {
                NameValueCollection postdata = new NameValueCollection();
                foreach (PropertyInfo p in typeof(T).GetProperties())
                {
                    if (obj.GetType().GetProperty(p.Name).GetValue(obj) != null)
                        if (obj.GetType().GetProperty(p.Name).GetValue(obj).ToString() != "0")
                            postdata.Add(p.Name, obj.GetType().GetProperty(p.Name).GetValue(obj).ToString());
                }
                String pagesource = Encoding.UTF8.GetString(client.UploadValues(url, "POST", postdata));
            }
        }
    }
}

